// ignore_for_file: avoid_unnecessary_containers, prefer_const_constructors, argument_type_not_assignable_to_error_handler

 import 'dart:io';
 import 'dart:typed_data';

 import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
 import 'package:crop_your_image/crop_your_image.dart';

 import 'package:path_provider/path_provider.dart';
import 'package:junyeyishu/utils/utils.dart';
 import 'package:junyeyishu/widgets/QGWidget.dart';

class QGMineAvatarClipPage extends StatefulWidget {
  Uint8List imageByte;
  QGMineAvatarClipPage({super.key, required this.imageByte});

  @override
  State<QGMineAvatarClipPage> createState() => _QGMineAvatarClipPageState();
}

class _QGMineAvatarClipPageState extends State<QGMineAvatarClipPage> {
  final _controller = CropController();

  @override
  Widget build(BuildContext context) {
    return QGScaffold(
      title: '个人信息',
      bgColor: Color(0xFFF8F8F6),
      body: Stack(
        children: [
          Container(
            child: Column(
              children: [
                SizedBox(
                  height: 97.rpx,
                ),
                Container(
                  width: kScreenWidth,
                  height: kScreenHeight * 0.5,
                  child: Crop(
                      image: widget.imageByte,
                      controller: _controller,
                      // initialSize: 0.9,
                      baseColor: Color(0xFFF8F8F6),
                      onCropped: (image) async{
                       uploadImage(image);
                      },
                      aspectRatio: 1),
                ),
              ],
            ),
          ),
          Positioned(
            left: 0.rpx,
            bottom: 0.rpx,
            child: Container(
              width: kScreenWidth,
              height: 95.rpx,
              padding: EdgeInsets.fromLTRB(5.rpx, 10.rpx, 5.rpx, 0.rpx),
              color: Color(0xFFFFFFFF),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  TextButton(
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                      child: Text(
                        '取消',
                        style: TextStyle(
                            fontSize: 16.rpx,
                            color: Color.fromRGBO(17, 17, 17, 0.6)),
                      )),
                  TextButton(
                    onPressed: () {
                      _controller.crop();
                    },
                    child: Text(
                      '确定',
                      style:
                          TextStyle(fontSize: 16.rpx, color: Color(0xFF111111)),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  uploadImage(imageByte) async {
    final tempDir = await getTemporaryDirectory();
    final file = await new File('${tempDir.path}/image.jpg').create();
    file.writeAsBytesSync(imageByte);
    upload(file);
  }

  //上传头像
  upload(File file) async {
    print(88888);
    print(file.path);
    final formData = FormData.fromMap({
      'avatar': await MultipartFile.fromFile(file.path),
    });

    Network.post(url_u_modifyAvatar, data: formData, context: context)
        .then((res) {
      if (res.success) {
        eventBus.fire(AvatarEvent(avatar: res.data["avatar"]));
        Navigator.of(context).pop();
      }
      print(res); 
      toast(res.msg);//success:false  msg:uncaught exception  data:null
    });
  }
}
